//
//  SecondView.m
//  相册罚写2
//
//  Created by dllo on 15/11/18.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SecondView.h"
#import "SecondScrollView.h"

@interface SecondView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, assign) NSInteger persentPage;

@end

@implementation SecondView

- (void)dealloc
{
    [_scrollV release];
    [_pageC release];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.persentPage = 1;
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollV.contentSize = CGSizeMake(self.frame.size.width * 21, 0);
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollV.pagingEnabled = YES;
    self.scrollV.delegate = self;
    [self addSubview:self.scrollV];
    [_scrollV release];
    

    
    for (NSInteger i = 0; i < 21; i++) {

        SecondScrollView *secondSV = [[SecondScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height) index:i];
        [self.scrollV addSubview:secondSV];
        [secondSV release];
    }
    
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 300, 300, 30)];
    
    self.pageC.numberOfPages = 19;
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.pageC];
    [_pageC release];
    
    
    
    
}

- (void)pageAction:(UIPageControl *)sender
{


    self.scrollV.contentOffset = CGPointMake(self.frame.size.width * (sender.currentPage + 1), 0);
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollV) {
        
        NSInteger temp = scrollView.contentOffset.x / self.frame.size.width;
        
        if (0 == temp) {
            scrollView.contentOffset = CGPointMake(self.frame.size.width * 19, 0);
            temp = 19;
        } else if (20 == temp) {
            scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
            temp = 1;
        }
        
        if (temp != self.persentPage) {
            [[self.scrollV.subviews objectAtIndex:self.persentPage] setZoomScale:1];
        
        self.pageC.currentPage = temp - 1;
        
        self.persentPage = temp;
        
        }
        
        
        
        
        
        
        
        
    }
    
 
    
}


@end
